package com.stone.customer.console.runner;

import com.stone.customer.core.domain.entity.Customer;
import com.stone.starter.core.runner.AbstractCommonCommandLineRunner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 会员信息错误数据消费Runner
 *
 * @author Mr_wenpan@163.com 2021/07/15 20:50
 */
@Slf4j
//@Component
public class CustomerInfoQueueConsumerRunner extends AbstractCommonCommandLineRunner<List<Customer>> {

    @Override
    protected void executeTask(List<Customer> data) {

        // 执行任务
        // 1、校验数据
        log.debug("接收到数据，马上进行xxx处理，数据：{}", data);

        // 2、写入数据库

        // 3、更新相关状态

        try {
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            log.error("线程睡眠被中断，中断异常：{}", e);
        }

        log.info("处理数据完成，处理的的数据集合大小为：{}", data.size());

    }

    @Override
    protected List<Customer> getData() {
        // 模拟获取到许多要处理的用户数据
        return new ArrayList<Customer>() {
            {
                add(Customer.builder().customerName("wenpan").build());
                add(Customer.builder().customerName("张三").build());
                add(Customer.builder().customerName("李四").build());
            }
        };
    }

    @Override
    protected boolean isEmpty(List<Customer> data) {
        return CollectionUtils.isEmpty(data);
    }

    @Override
    protected long getSleepMillis() {
        return 10;
    }
}
